﻿using MyCode.Project.Domain.Message.Response.User;
using MyCode.Project.Infrastructure.Common;
using MyCode.Project.Infrastructure.Extensions;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyCode.Project.Domain.Businesses.Menus
{
    /// <summary>
    /// 菜单模块
    /// </summary>
    public class MenuModule
    {
        /// <summary>
        /// 模块ID
        /// </summary>
        public Guid? Id { get; set; }

        /// <summary>
        /// 菜单ID
        /// </summary>
        public Guid? MenuId { get; set; }

        /// <summary>
        /// 功能类型。1:WebApi,2:函数,3:存储过程
        /// </summary>
        public int FuncType { get; set; }

        /// <summary>
        /// 路径
        /// </summary>
        public string Path { get; set; }

        /// <summary>
        /// 功能点
        /// </summary>
        [JsonIgnore]
        public string FuncPoint { get; set; }

        /// <summary>
        /// 菜单按钮
        /// </summary>
        public MenuButton MenuPower { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        public string Note { get; set; }

        /// <summary>
        /// 系统类型。0:公用,11:公司系统,12:加盟商系统,13:店铺系统
        /// </summary>
        public int SystemType { get; set; }

        /// <summary>
        /// 初始化菜单按钮
        /// </summary>
        public void InitPower()
        {
            if (FuncPoint.IsEmpty())
            {
                return;
            }
            this.MenuPower = JsonHelper.ToObject<MenuButton>(FuncPoint);
        }

        /// <summary>
        /// 转换成菜单按钮
        /// </summary>
        /// <param name="isCustomer">是否加盟商</param>
        /// <returns></returns>
        public MenuButtonView ToMenuButtonView(bool isCustomer)
        {
            if (MenuPower == null)
            {
                return null;
            }
            MenuButtonView button = new MenuButtonView();
            if (MenuPower.Page.Any())
            {
                button.Page = MenuPower.Page.Where(x => !isCustomer || x.SysType == 0).Select(x => x.Name).ToList();
            }
            if (MenuPower.PageList.Any())
            {
                button.PageList = MenuPower.PageList.Where(x => !isCustomer || x.SysType == 0).Select(x => x.Name).ToList();
            }
            if (MenuPower.ChildrenList.Any())
            {
                button.ChildrenList = MenuPower.ChildrenList.Where(x => !isCustomer || x.SysType == 0).Select(x => x.Name).ToList();
            }

            return button;
        }
    }
}
